import {
  getShopCarList
} from "../../utils/api";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    empflag: 1,
    total:false, //是否全选
    totalPrice:0, //总价
    list:[],
    // cart_list: [{ id: 0, select: false, src: "/images/img/pic-13.png", title: "黑色t许经典白", color: "黑色", size: "170/55A", price: "560", num: "2" }, { id: 1, select: false, src: "/images/img/pic-13.png", title: "黑色2许经典白", color: "白色", size: "170/55A", price: "560", num: "2" }, { id: 2, select: false, src: "/images/img/pic-13.png", title: "黑色2许经典白", color: "白色", size: "170/55A", price: "560", num: "1" }],
    cart_list: [],
    isEdit:false,
    statusBarHeight: 0,
    navigationBarHeight: 0
  },
  backHome: function () {
    wx.reLaunch({
      url: '../index/index',
    })
  },
  back: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  orderbug: function () {
    wx.navigateTo({
      url: '../orderbuy/index',
    })
  },
  totalPrice(){//计算总价
    let that = this
    let price = 0
    let flag = true
    for (let i = 0; i < that.data.cart_list.length;i++){
      if (that.data.cart_list[i].select){
        price = price + that.data.cart_list[i].price * that.data.cart_list[i].num
      }else{
        flag = false
      }
    }
    this.setData({
      totalPrice: price.toFixed(2),
      total: flag
    })

  },
  totalFun(){ //全选
    this.data.total = !this.data.total
    this.data.cart_list.map((v,k)=>{
      if (this.data.total) {
        v.select = true
      } else {
        v.select = false
      }
    })
    this.setData({
      cart_list: this.data.cart_list,
      total: this.data.total
    })
    this.totalPrice()
  },
  labelFun(e){//单选
    let temp_select = 'cart_list[' + e.currentTarget.dataset.index + '].select'
    this.setData({
      [temp_select]: this.data.cart_list[e.currentTarget.dataset.index].select ? false : true})
    this.totalPrice()
  },
  plusFun(e){ //增加商品数量
    let temp_num = 'cart_list[' + e.currentTarget.dataset.index + '].num'
    let temp_num1 = this.data.cart_list[e.currentTarget.dataset.index].num
    if (temp_num1 < 0) {
      console.log('====++++=====')
    } else {
      temp_num1++
    }
    this.setData({
      [temp_num]: temp_num1
    })
    this.setData({
      [temp_num]: temp_num1
    })
    this.totalPrice()
  },
  reduceFun(e) { //减少商品数量
    let temp_num = 'cart_list[' + e.currentTarget.dataset.index + '].num'
    let temp_num1 = this.data.cart_list[e.currentTarget.dataset.index].num
    if (temp_num1 < 1){
      console.log('====++++=====')
    }else{
      temp_num1--
    }
    this.setData({
      [temp_num]: temp_num1
    })
    this.totalPrice()
  },
  delItemFun(e){ //删除单商品
    let tmp_index = e.currentTarget.dataset.index
    let tmp_cart_list = this.data.cart_list
    if (tmp_cart_list.length >= (tmp_index+1)){
      tmp_cart_list.splice(tmp_index,1)
    }
    this.setData({
      cart_list: tmp_cart_list
    })
    this.totalPrice()
  },
  //测试空页面
  tesEmpty: function () {
    wx.navigateTo({
      url: `/pages/goods/index`,
    })
  },
    
  async getShopCarList(){
    const res = await getShopCarList()
    if (res.code === 200) {
       this.setData({
        cart_list:res.data
       })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     this.getShopCarList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})